"""
姓名：邬昱初
学号：20202321
时间：2021年6月28日
"""
import requests
import csv
from lxml import etree
def writecsv(item):
    with open('hero.csv','a',encoding='utf-8') as f:
        writer=csv.writer(f)
        try:
            writer.writerow(item)
        except:
            print('write error!')

url = 'https://heroes.blizzard.cn/heroes'
headers = \
    {
     'ser-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36'
}
html=requests.get(url,headers=headers)
selector=etree.HTML(html.text)
hero_list=selector.xpath('//*[@id="container"]/div[2]/div[2]/div')
for hero in hero_list:
    name=hero.xpath('a/h4/text()')[0]
    chenghao=hero.xpath('a/div[2]/i/text()')[0]
    item=[chenghao,name]
    writecsv(item)
print('提取：',name)


